package com.cn.jinl.service;

import com.cn.jinl.api.ISysItemService;
import com.cn.jinl.dao.SysItemMapper;
import com.cn.jinl.domain.SysItem;
import com.cn.jinl.domain.SysItemExample;
import com.cn.jinl.utils.CollectionsUtil;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.Constants;
import com.cn.jinl.utils.QueryForm;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @类名:SysItemServiceImpl
 * @包名:com.cn.device.service
 * @作者:chenxingliang
 * @时间:2022-11-23 13:38
 * @版本:1.0.0
 */
@Service
public class SysItemServiceImpl implements ISysItemService {

    @Autowired
    private SysItemMapper sysItemMapper;

    @Override
    public List<SysItem> getMgItemList(QueryForm queryForm) throws Exception {
        return sysItemMapper.selectSysItems(queryForm);
    }

    @Override
    public List<SysItem> getMgItemSelect(QueryForm queryForm) throws Exception {
        List<String> types = queryForm.getTypes();
        if(CollectionsUtil.isEmpty(types)){
            return new ArrayList<>();
        }
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeIn(types).andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("item_name asc");
        List<SysItem> list = sysItemMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<SysItem> getMgItemByType(String type) throws Exception {
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeEqualTo(type).andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS);
        return sysItemMapper.selectByExample(example);
    }



    @Override
    public List<SysItem> getAllMgItemList() throws Exception {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS);
        List<SysItem> list = sysItemMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<SysItem> getConcatUsers() throws Exception {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS).andItemTypeEqualTo("16");
        List<SysItem> list = sysItemMapper.selectByExample(example);
        return list;
    }

    @Override
    public Map<String, String> getMgItemNameMap(QueryForm queryForm) throws Exception {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotEmpty(queryForm.getType())){
            criteria.andItemTypeEqualTo(queryForm.getType());
        }
        List<String> types = queryForm.getTypes();
        if(CollectionsUtil.isNotEmpty(types)){
            criteria.andItemTypeIn(types);
        }
        criteria.andItemStatusEqualTo(Constants.NORMAL_STATUS);
        List<SysItem> list = sysItemMapper.selectByExample(example);
        Map<String,String> result = Maps.newHashMap();
        for(SysItem mgItem:list){
            result.put(mgItem.getId(),mgItem.getItemName());
        }
        return result;
    }

    @Override
    public String addMgItemDetail(SysItem mgItem) throws Exception {
        String itemName = mgItem.getItemName();
        String itemType = mgItem.getItemType();
        if(!StringUtils.equals(mgItem.getRepeat(),"yes")){
            SysItemExample example = new SysItemExample();
            example.createCriteria().andItemTypeEqualTo(itemType).andItemNameEqualTo(itemName).andStatusEqualTo(Constants.NORMAL_STATUS);
            int existCount = sysItemMapper.countByExample(example);
            if(existCount > 0){
                return "名称重复！";
            }
        }
        mgItem.setId(CommonUtil.generateRandomNum("item-"));
        int count = sysItemMapper.insertSelective(mgItem);
        return CommonUtil.outStr(count);
    }

    @Override
    public String getMgItemDefaultValue(SysItem mgItem) throws Exception {
        String itemType = mgItem.getItemType();
        String itemName = mgItem.getItemName();
        if(StringUtils.isEmpty(itemType)|| StringUtils.isEmpty(itemName)){
            return "";
        }
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeEqualTo(itemType).andItemNameEqualTo(itemName).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<SysItem> list = sysItemMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)||list.size()!=1){
            return "";
        }
        return list.get(0).getId();
    }

    @Override
    public int countMgItemByName(String name, String type, String id) throws Exception {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andItemNameEqualTo(name).andItemTypeEqualTo(type);
        if(StringUtils.isNotBlank(id)){
            criteria.andIdNotEqualTo(id);
        }
        int count = sysItemMapper.countByExample(example);
        return count;
    }

    @Override
    public int countMgItemByCode(String code, String type, String id) throws Exception {
        SysItemExample example = new SysItemExample();
        SysItemExample.Criteria criteria = example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andItemCodeEqualTo(code).andItemTypeEqualTo(type);
        if(StringUtils.isNotBlank(id)){
            criteria.andIdNotEqualTo(id);
        }
        int count = sysItemMapper.countByExample(example);
        return count;
    }

    @Override
    public String updateMgItemDetail(SysItem mgItem) throws Exception {
        String id = mgItem.getId();
        if(StringUtils.isBlank(id)){
            return "updateMgItemDetail id null";
        }
        String itemName = mgItem.getItemName();
        String itemType = mgItem.getItemType();
        if(this.countMgItemByName(itemName,itemType,id) > 0){
            return "名称重复！";
        }
        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdEqualTo(id);
        int count = sysItemMapper.updateByExampleSelective(mgItem,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateMgItemCodeDetail(SysItem mgItem) throws Exception {
        String id = mgItem.getId();
        if(StringUtils.isBlank(id)){
            return "updateMgItemCodeDetail id null";
        }
        String itemCode = mgItem.getItemCode();
        String itemType = mgItem.getItemType();
        if(this.countMgItemByCode(itemCode,itemType,id) > 0){
            return "挤塑机号重复！";
        }
        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdEqualTo(id);
        int count = sysItemMapper.updateByExampleSelective(mgItem,example);
        return CommonUtil.outStr(count);
    }


    @Override
    public String updateMgItemValue(SysItem mgItem) throws Exception {
        String id = mgItem.getId();
        if(StringUtils.isBlank(id)){
            return "updateMgItemValue id null";
        }
        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdEqualTo(id);
        int count = sysItemMapper.updateByExampleSelective(mgItem,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteMgItemDetail(QueryForm queryForm) throws Exception {
        List<String> ids = queryForm.getIds();
        if(CollectionsUtil.isEmpty(ids)){
            throw new Exception("deleteMgItemDetail ids 为空！");
        }
        SysItem update = new SysItem();
        update.setStatus(Constants.DEL_STATUS);
        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdIn(ids);
        int count = sysItemMapper.updateByExampleSelective(update,example);
        return  CommonUtil.outStr(count);
    }

    @Override
    public SysItem getSysItemInfoById(String id) throws Exception {
        SysItemExample example = new SysItemExample();
        example.createCriteria().andIdEqualTo(id).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<SysItem> systems = sysItemMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(systems)||systems.size() != 1){
            return null;
        }
        return systems.get(0);
    }

    @Override
    public String addSysItemByNotRepeat(SysItem mgItem) throws Exception {
        String itemName = mgItem.getItemName();
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemNameEqualTo(itemName).andStatusEqualTo(Constants.NORMAL_STATUS).andItemTypeEqualTo(mgItem.getItemType());
        int count = sysItemMapper.countByExample(example);
        if(count > 0){
            return "保养项目已经存在！";
        }
        mgItem.setId(CommonUtil.generateRandomNum("item-"));
        count = sysItemMapper.insertSelective(mgItem);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<SysItem> getMgItemSelectByType(String type) throws Exception {
        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeEqualTo(type).andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("create_time asc");
        List<SysItem> list = sysItemMapper.selectByExample(example);
        return list;
    }

    @Override
    public Map<String, List<SysItem>> getCategoryMap() throws Exception {
        Map<String, List<SysItem>> map = new HashMap<>();

        SysItemExample example = new SysItemExample();
        example.createCriteria().andItemTypeEqualTo("1").andStatusEqualTo(Constants.NORMAL_STATUS).andItemStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("item_name asc");
        List<SysItem> list = sysItemMapper.selectByExample(example);
        for (SysItem item : list){
            String itemName = item.getItemName();
            if (map.containsKey(itemName)){
                List<SysItem> exeList = map.get(itemName);
                exeList.add(item);
            } else {
                List<SysItem> unexeList = new ArrayList<>();
                unexeList.add(item);
                map.put(itemName,unexeList);
            }
        }

        return map;
    }

    @Override
    public String addMgItemCodeDetail(SysItem mgItem) throws Exception {
        String itemCode = mgItem.getItemCode();
        String itemType = mgItem.getItemType();
        int existCount = this.countMgItemByCode(itemCode,itemType,"");
        if(existCount > 0){
            return "挤塑机号已存在！";
        }
        mgItem.setId(CommonUtil.generateRandomNum("item-"));
        int count = sysItemMapper.insertSelective(mgItem);
        return CommonUtil.outStr(count);
    }
}
